package MultiThread.test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class SequenceExe {
    //3个线程依次执行
//    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
    private static AtomicInteger one = new AtomicInteger(1);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Worker2(one,"A"));
        Thread t2 = new Thread(new Worker2(one,"B"));
        Thread t3 = new Thread(new Worker2(one,"C"));

        t1.start();
        t2.start();
        t3.start();
    }

}
class Worker2 implements Runnable{
    private AtomicInteger atomicInteger;
    private String  content;

    public Worker2(AtomicInteger atomicInteger, String content) {
        this.atomicInteger = atomicInteger;
        this.content = content;
    }

    @Override
    public void run() {
        if(content.equals("A")) {
            for (int i = 0; i < 10; i++) {
                while (atomicInteger.get() !=1) {
                }
                System.out.println("A");
                atomicInteger.getAndIncrement();
            }
        }else if(content.equals("B")) {
            for (int i = 0; i < 10; i++) {
                while (atomicInteger.get() !=2) {
                }
                System.out.println("B");
                atomicInteger.getAndIncrement();
            }
        }else if(content.equals("C")) {
            for (int i = 0; i < 10; i++) {
                while (atomicInteger.get() !=3) {
                }
                System.out.println("C");
                atomicInteger.compareAndSet(3,1);
            }
        }
    }
}
